<?php
	class Menu extends CI_Controller {
	   
       function __construct() {
	       parent::__construct();
           $user = $this->session->userdata('user');
           if (!$user) redirect('main/alogin');
           $this->load->model('Dataset_db');
           $this->load->model('menu/Menu_db','Menu');
	   }
       
       function addmenugroup() {         
           if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
           echo "You do not have permission to view this page"; exit; }
           $data = array();
           if( isset( $_POST["btnAddMenuGroup"] ) ) {
                $this->form_validation->set_rules('mglabel', 'Label', 'trim|required|min_length[3]|max_length[20]|xss_clean');
                $this->form_validation->set_rules('mgurl', 'Url', 'trim|required|min_length[3]|max_length[12]|xss_clean');
                $this->form_validation->set_rules('mgposition', 'Position', 'trim|required|numeric|xss_clean');
                if ($this->form_validation->run() == FALSE) {
                    $data["errors"] = $this->form_validation->error_array();
                } else {
                    $pdata = array();
                    $fields = array(
    						"mglabel"
    						,"mgurl"
    						,"mgposition"				
    						);
                    foreach ($fields as $field) {
        				if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field,true);
        			}
                    $data["pdata"] = $pdata;
                    //print_object($pdata); exit;
                    if( $this->Menu->is_mg_duplicate($pdata['mgurl'])) {
                        $data["errors"] = array("Menu Url already exist. Choose different one."); 
                    } else {
                        $save_result = $this->Menu->save_mg($pdata);
                        if($save_result['result'] == 'S') {
                            $data["successes"] = array('Menu group added successfully.');
                            unset($data["pdata"]);
                        } else {
                            $data["errors"] = array($save_result['error_message']);   
                        }
                    }   
                }
           }
           
            $data['ptitle'] = "Add Menu Group";
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $this->load->view('addmenugroup',$data); 
       }
       
       function editmenugroup() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            $data['ptitle'] = "Edit Menu Groups";
            if( isset( $_POST["btnUpdateMenuGroup"] ) ) {
                //print_object($_POST);exit;
                $this->form_validation->set_rules('mglabel', 'Label', 'trim|required|min_length[3]|max_length[20]|xss_clean');
                $this->form_validation->set_rules('mgurl', 'Url', 'trim|required|min_length[3]|max_length[12]|xss_clean');
                $this->form_validation->set_rules('mgposition', 'Position', 'trim|required|numeric|xss_clean');
                $this->form_validation->set_rules('mgid', 'Menu Group ID', 'trim|required|numeric|xss_clean');
                if ($this->form_validation->run() == FALSE) {
                    $data["errors"] = $this->form_validation->error_array();
                } else {
                    $pdata = array();
                    $fields = array(
    						"mglabel"
    						,"mgurl"
    						,"mgposition"				
    						);
                    foreach ($fields as $field) {
        				if (isset($_POST[$field])) $pdata[$field] = $this->input->post($field,true);
        			}
                    
                    //print_object($this->input->post('mgid',true));print_object($pdata); exit;
                    $save_result = $this->Menu->save_mg($pdata,$this->input->post('mgid',true));
                    //print_object($save_result); exit;
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('Menu group updated successfully.');
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
            } 
            
            if( isset( $_POST["mgid"] ) ) {
                $mg_result = $this->Menu->get_mg(array('mgid'=> $this->input->post("mgid",true)));
                //print_object($mg_result);exit;
                if(count($mg_result) == 0) {
                    $data['errors'] = "Could not find the selected Menu Group";
                } else {
                    $data['mgid'] = $mg_result[0]['mgid'];
                    $data['mglabel'] = $mg_result[0]['mglabel'];
                    $data['mgposition'] = $mg_result[0]['mgposition'];
                    $data['mgurl'] = $mg_result[0]['mgurl'];
                }
            }
            
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['dgdata'] = $this->Menu->build_mg_page_data();
            //print_object($data); exit;
            $data['content'] = $this->load->view('editmenugroup',$data,true);
            $this->load->vars($data);
            $this->load->view('admin/default_view'); 
       }
       
       function deletemenugroup() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            //print_object($_POST);exit;
            $data['ptitle'] = "Delete Menu Groups";
            if( isset( $_POST["mgid"] ) ) {
                $mg_result = $this->Menu->delete_mg(array('mgid'=> $this->input->post("mgid",true)));
                //print_object($mg_result);exit;
                if(!$mg_result) {
                    $data["errors"] = array('Could not find the selected Menu Group.');
                } else {
                    $data["successes"] = array('Menu group deleted successfully.');
                }
            }
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['dgdata'] = $this->Menu->build_mg_page_data();
            //print_object($data); exit;
            $data['content'] = $this->load->view('viewmenugroup',$data,true);
            $this->load->vars($data);
            $this->load->view('admin/default_view'); 
       }
       
       function viewmenugroup() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            $data['ptitle'] = "View Menu Groups";
            if( isset( $_POST["mgid"] ) ) {
                $mg_result = $this->Menu->get_mg(array('mgid'=> $this->input->post("mgid",true)));
                //print_object($mg_result);exit;
                if(count($mg_result) == 0) {
                    $data['errors'] = "Could not find the selected Menu Group";
                } else {
                    $data['mgid'] = $mg_result[0]['mgid'];
                    $data['mglabel'] = $mg_result[0]['mglabel'];
                    $data['mgposition'] = $mg_result[0]['mgposition'];
                    $data['mgurl'] = $mg_result[0]['mgurl'];
                }
            }
            
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            $data['dgdata'] = $this->Menu->build_mg_page_data();
            //print_object($data); exit;
            $this->load->view('viewmenugroup',$data);
       }
       
       function viewmenuitem( $itemid="" ) {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            if($itemid!="") {
                $itemid = xss_clean($itemid);
                $mi_result = $this->Menu->get_mi(array('miid'=> $itemid));
                if(count($mi_result) == 0) {
                    $data['errors'] = "Could not find the selected Menu Item";
                } else {
                    $data['miid'] = $mi_result[0]['miid'];
                    $data['mgid'] = $mi_result[0]['mgid'];
                    $data['mglabel'] = $mi_result[0]['mglabel'];
                    $data['milabel'] = $mi_result[0]['milabel'];
                    $data['miposition'] = $mi_result[0]['miposition'];
                    $data['miurl'] = $mi_result[0]['miurl'];
                    $data['miactive'] = $mi_result[0]['miactive'];
                    $data['mihidden'] = $mi_result[0]['mihidden'];
                    $miroles = $this->Menu->get_mi_roles(array('miid'=> $mi_result[0]['miid']));
                    $data['miroles'] = $miroles;
                }
                $data['roles'] = $this->Menu->get_user_roles();
                $data['menugroups'] = $this->Menu->get_menu_groups();
                $navResult = $this->Dataset_db->buildNav();
                $data['navs'] = $navResult['links'];
                $data['clabel'] = $navResult['selected']['group'];            
                $data['mlabel'] = $navResult['selected']['item'];
                //print_object($data); exit;
                $this->load->view('viewsinglemenuitem',$data);
            } else {
                $data['ptitle'] = "View Menu Items";
                $data['dgdata'] = $this->Menu->build_mi_dg_data();    
                //print_object($data);exit;
                $navResult = $this->Dataset_db->buildNav();
                $data['navs'] = $navResult['links'];
                $data['clabel'] = $navResult['selected']['group'];            
                $data['mlabel'] = $navResult['selected']['item'];
                //print_object($data); exit;
                $this->load->view('viewmenuitem',$data);
            }

       }
       
       function searchmenuitem() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            $data['ptitle'] = "Search Menu Items";
            
            $filterdata = array(
                'items' => array(
                    array(
                        'label' => 'Menu Item',
                        'table' => 'mi',
                        'name' => 'milabel',
                        'value' => isset( $_POST["milabel"] ) ? $_POST["milabel"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Menu Group',
                        'table' => 'mi',
                        'name' => 'mgid',
                        'value' => isset( $_POST["mgid"] ) ? $_POST["mgid"] : '',
                        'type' => 'select',
                        'data' => $this->Menu->get_menu_groups_list()
                    ),
                    array(
                        'label' => 'Menu Url',
                        'table' => 'mi',
                        'name' => 'miurl',
                        'value' => isset( $_POST["miurl"] ) ? $_POST["miurl"] : '',
                        'type' => 'text',
                        'data' => ''
                    ),
                    array(
                        'label' => 'Active',
                        'table' => 'mi',
                        'name' => 'miactive',
                        'value' => isset( $_POST["miactive"] ) ? $_POST["miactive"] : '',
                        'type' => 'checkbox',
                        'data' => array( array( 'label' => 'True', 'value' => '1' ), array( 'label' => 'False', 'value' => '0' ) )
                    ),
                    array(
                        'label' => 'Hidden',
                        'table' => 'mi',
                        'name' => 'mihidden',
                        'value' => isset( $_POST["mihidden"] ) ? $_POST["mihidden"] : '',
                        'type' => 'checkbox',
                        'data' => array( array( 'label' => 'True', 'value' => '1' ), array( 'label' => 'False', 'value' => '0' ) )
                    )
                ),
                'action' => '/menu/searchmenuitem',
                'submit' => 'btnSearchMenuItem'
            );
            
            if($this->input->post('show_all')) {
                $data['show_all'] = true;
            }
            
            $data['dgdata'] = $this->Menu->build_mi_dg_data($filterdata);
            
            $data['filterdata'] = $filterdata;
               
            //print_object($data);exit;
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $this->load->view('searchitem',$data);
       }
       
       function addmenuitem() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)) {
            echo "You do not have permission to view this page"; exit; }
            $data['ptitle'] = "Add Menu Item";
            $data['roles'] = $this->Menu->get_user_roles();
            if( isset( $_POST["btnAddMenuItem"] ) ) {
                //print_object($_POST); exit;
                $this->form_validation->set_rules('mgid', 'Menu Group', 'trim|required|xss_clean');
                $this->form_validation->set_rules('milabel', 'Menu Item Label', 'trim|required|min_length[3]|max_length[20]|xss_clean');
                $this->form_validation->set_rules('miurl', 'Url', 'trim|required|min_length[3]|max_length[20]|xss_clean');
                $this->form_validation->set_rules('miposition', 'Position', 'trim|required|numeric|xss_clean');
                
                $pdata = array();
                $fields = array(
						"mgid"
						,"milabel"
						,"miurl"
                        ,"miposition"
                        ,"miactive"
                        ,"mihidden"				
						);
                foreach ($fields as $field) {
    				if (isset($_POST[$field])) {
    				    $pdata[$field] = $this->input->post($field,true);
    				} else { $data[$field] = ""; }   
    			}
                
                $data["postdata"] = $pdata;
                $proles = $this->input->post('miroles',true); 
                $data['postdata']['miroles'] = $proles;
                if ($this->form_validation->run() == FALSE) {
                    $data["errors"] = $this->form_validation->error_array();
                } else {
                    //print_object($pdata); exit;
                    if( $this->Menu->is_mi_duplicate($pdata['mgid'], $pdata['miurl']) ) {
                        $data["errors"] = array("Menu Item Url already exist. Choose different one."); 
                    } else {
                        $save_result = $this->Menu->save_mi($pdata);
                        if($save_result['result'] == 'S') {
                            $data["successes"] = array('Menu item added successfully.');
                            $miid = $save_result['insert_id'];
                            $access_data = array();
                            if(count($proles) >0) {
                                foreach ($data['roles'] as $role) {
                                    if(in_array($role['roleid'], $proles)) {
                                        $in_data = array('roleid' => $role['roleid'], 'miid' => $miid, 'allowed' => 1);
                                    } else {
                                        $in_data = array('roleid' => $role['roleid'], 'miid' => $miid, 'allowed' => 0);
                                    }
                                    array_push($access_data,$in_data);
                                }
                                $this->Menu->save_mi_access($access_data);    
                            }
                            $data['postdata'] = "";
                        } else {
                            $data["errors"] = array($save_result['error_message']);   
                        }    
                    }                       
                }
           }
            
            $navResult = $this->Dataset_db->buildNav();
            
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            
            $data['menugroups'] = $this->Menu->get_menu_groups();
            //print_object($data); exit;
            $this->load->view('addmenuitem',$data);
       }
       
       function editmenuitem() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            //print_object($_POST); exit;
            $data['ptitle'] = "Edit Menu Item";
            $data['roles'] = $this->Menu->get_user_roles();
            if( isset( $_POST["btnUpdateMenuItem"] ) ) {
                $this->form_validation->set_rules('mgid', 'Menu Group', 'trim|required|xss_clean');
                $this->form_validation->set_rules('milabel', 'Menu Item Label', 'trim|required|min_length[3]|max_length[20]|xss_clean');
                $this->form_validation->set_rules('miurl', 'Url', 'trim|required|min_length[3]|max_length[20]|xss_clean');
                $this->form_validation->set_rules('miposition', 'Position', 'trim|required|numeric|xss_clean');
                
                $pdata = array();
                $fields = array(
						 "mgid"
						,"milabel"
						,"miurl"
                        ,"miposition"
                        ,"miactive"
                        ,"mihidden"				
						);
                foreach ($fields as $field) {
    				if (isset($_POST[$field])) {
    				    $pdata[$field] = $this->input->post($field,true);
    				} else { $data[$field] = ""; }   
    			}
                
                $data["postdata"] = $pdata;
                $proles = $this->input->post('miroles',true); 
                $data['postdata']['miroles'] = $proles;
                if ($this->form_validation->run() == FALSE) {
                    $data["errors"] = $this->form_validation->error_array();
                } else {
                    //print_object($pdata); exit;
                    $save_result = $this->Menu->save_mi($pdata,$this->input->post('miid',true));
                    if($save_result['result'] == 'S') {
                        $data["successes"] = array('Menu item updated successfully.');
                        $miid = $this->input->post('miid',true);
                        $access_data = array();
                        if(count($proles) >0){
                            foreach ($data['roles'] as $role) {
                                if(in_array($role['roleid'], $proles)) {
                                    $in_data = array('roleid' => $role['roleid'], 'miid' => $miid, 'allowed' => 1);
                                } else {
                                    $in_data = array('roleid' => $role['roleid'], 'miid' => $miid, 'allowed' => 0);
                                }
                                array_push($access_data,$in_data);
                            }
                            $this->Menu->save_mi_access($access_data,$miid);    
                        }
                        $data['postdata'] = "";
                    } else {
                        $data["errors"] = array($save_result['error_message']);   
                    }   
                }
           } 
           
           if( isset( $_POST["miid"] ) ) {
                $mi_result = $this->Menu->get_mi(array('miid'=> $this->input->post("miid",true)));
                //print_object($mi_result);exit;
                if(count($mi_result) == 0) {
                    $data['errors'] = "Could not find the selected Menu Item";
                } else {
                    $data['miid'] = $mi_result[0]['miid'];
                    $data['mgid'] = $mi_result[0]['mgid'];
                    $data['mglabel'] = $mi_result[0]['mglabel'];
                    $data['milabel'] = $mi_result[0]['milabel'];
                    $data['miposition'] = $mi_result[0]['miposition'];
                    $data['miurl'] = $mi_result[0]['miurl'];
                    $data['miactive'] = $mi_result[0]['miactive'];
                    $data['mihidden'] = $mi_result[0]['mihidden'];
                    $miroles = $this->Menu->get_mi_roles(array('miid'=> $mi_result[0]['miid']));
                    $data['miroles'] = $miroles;
                }
            } else {
                $data["errors"] = array("No or invalid menu item.");        
            }
            
            //print_object($data); exit;
            $navResult = $this->Dataset_db->buildNav();
            
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            
            $data['menugroups'] = $this->Menu->get_menu_groups();
            
            $data['content'] = $this->load->view('editmenuitem',$data);
            //print_object($data['menugroups']); exit;
            //$this->load->vars($data);
            //$this->load->view('admin/default_view'); 
       }
       
       function deletemenuitem() {
            if( !$this->Dataset_db->check_access($this->router->class,$this->router->method)){
            echo "You do not have permission to view this page"; exit; }
            //print_object($_POST);exit;
            $data['ptitle'] = "Delete Menu Item";
            if( isset( $_POST["miid"] ) ) {
                $mg_result = $this->Menu->delete_mi(array('miid'=> $this->input->post("miid",true)));
                //print_object($mg_result);exit;
                if(!$mg_result) {
                    $data["errors"] = array('Could not find the selected Menu Item.');
                } else {
                    $data["successes"] = array('Menu item deleted successfully.');
                }
            }
            $navResult = $this->Dataset_db->buildNav();
            $data['navs'] = $navResult['links'];
            $data['clabel'] = $navResult['selected']['group'];            
            $data['mlabel'] = $navResult['selected']['item'];
            //print_object($data); exit;
            $data['content'] = $this->load->view('deleterow',$data,true);
            $this->load->vars($data);
            $this->load->view('admin/default_view'); 
       }       
       
	}

/* End of file */